package de.hdu.pms.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.model.Krankheit;
import de.hdu.pms.model.Mitarbeiter;

public class KrankheitDao extends HibernateDaoSupport {

	public static Logger Log = Logger.getLogger(KrankheitDao.class);

	public static void setLog(Logger log) {
		Log = log;
	}

	/**
	 * searches for an specific entry in the table Krankheit by primary key
	 * krankheit_id (primary key) returns all entries from the table Krankheit
	 * with a special id
	 * 
	 * @param krankheit_id
	 * @return Krankheit
	 * @author BBV
	 */

	public Krankheit findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Krankheit) template.get(Krankheit.class, id);
	}

	/**
	 * List with all entries from the table Krankheit of the user.
	 * 
	 * @returns results
	 * @param mit_id
	 * @author BBV
	 * 
	 */

	@SuppressWarnings("unchecked")
	public List<Krankheit> findByMitarbeiter(Integer mit_id) {
		HibernateTemplate template = getHibernateTemplate();
		List<Krankheit> results = template
				.find("from Krankheit k where k.mitarbeiter.id = " + mit_id);
		return results;
	}

	/**
	 * lists all entries from the table Krankheit
	 * 
	 * @returns all entries from the table Krankheit
	 * @see Krankheit
	 * @author BBV
	 */

	public List<Krankheit> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Krankheit.class);
	}

	/**
	 * saves a modified entry into the table Krankheit
	 * 
	 * @param krankheit
	 * @param id
	 * @return krankeit
	 * @see Krankheit
	 * @author BBV
	 */

	public Krankheit save(Krankheit krankheit, Integer id) {
		HibernateTemplate template = getHibernateTemplate();
		Mitarbeiter m = (Mitarbeiter) template.get(Mitarbeiter.class, id);
		krankheit.setMitarbeiter(m);
		template.saveOrUpdate(krankheit);
		return krankheit;
	}

	/**
	 * Function to delete entry in table Krankheit
	 * 
	 * @param krankheit
	 * @return void
	 * @see Krankheit
	 * @author BBV
	 */

	public void delete(Krankheit krankheit) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(krankheit);
	}
	
	/**
	 * Function to check Krankheit for a defined date
	 * @param datum
	 * @param mit_id
	 * @return boolean
	 * @author BBV
	 */

	public boolean checkDate(Date datum, Integer mit_id) {
		HibernateTemplate template = getHibernateTemplate();
		SimpleDateFormat formatDatum = new SimpleDateFormat("yyyy-MM-dd");
		String tmp = "'" + formatDatum.format(datum) + "'";
		@SuppressWarnings("unchecked")
		List<Krankheit> results = template
				.find("from Krankheit k where k.krankheit_von <= " + tmp
						+ " AND k.krankheit_bis >= " + tmp
						+ " AND k.mitarbeiter.id = " + mit_id);
		if (results.size() != 0)
			return true;
		else
			return false;
	}
}
